<%@page import="interface_layer.ErrorHandler"%>
<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<%@ page import="database_layer.*" %>
<%@ page import="java.util.*"%>
<%@ page import="processing_layer.*" %>
<%@ page errorPage="exceptionPage.jsp" %>
<%-- 
	This JSP represents all the courses that a user can register to. Of course,
	this page is only accessible to students. Additionally, it is usually not
	accessed directly, but by a button in the user page.
	The page allows not only seeing the possible courses, but gives an option to
	register to any of the courses.
 --%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255"/>
<link href="css/templatemo_style.css" rel="stylesheet" type="text/css" />
<title>Courses the student can register to</title>
</head>
<body>
<jsp:include page="header_part_1.html"/>
            <div id="templatemo_menu">
                <ul>
                    <li><a href="index.jsp">Home</a></li>
                    <li><a href="about.jsp">About Us</a></li>
<%
                    	String username = (String) session.getAttribute("username");
                        if (null != username)
                        {
%>
                            <li><a href="userPage.jsp">Your profile</a></li>
                    		<li><a href="allCourses.jsp" class="current">Courses</a></li>
                    		<li><a href="allUsers.jsp">Users</a></li>
<%
                        }
%>
                </ul>    	
            </div> <!-- end of templatemo_menu -->
<jsp:include page="header_part_2.html"/>
<div id="content">
<%
	// Need to add a session check here?
	String userType = (String) session.getAttribute("userType");
	
	if (null == username || null == userType)
	{
		/* Not logged in = Not authorized */
		ErrorHandler.parseForErrors(Common.UNAUTHORIZED_OPERATION, response, null);
		return;
	}
	
	if (!userType.equals(Common.studentType))
	{
		/* Admin's don't have courses... */
		ErrorHandler.parseForErrors(Common.UNAUTHORIZED_OPERATION, response, null);
		return;
	}
	
	ArrayList<Course> userAvailableCourses = null;
	
	userAvailableCourses = QueryDispatcher.getAllAvailableCourses(username);
	if (null == userAvailableCourses)
	{
		ErrorHandler.parseForErrors(Common.INTERNAL_DB_ERROR, response, null);
		return;
	}
	
	if (0 == userAvailableCourses.size())
	{
%>
	<i>Sorry, there are no available courses for you, please try again some other time...</i>
<%		
	}
	else
	{
%>
		<b><i>
		This list contains all the <b>available</b> courses for you<br/>
		You may choose to <u>Register</u> (You can always enter the course details and perform this action)<br/>
		</i></b>
		<br/>
		<table border="1">
<%
		for (Course c : userAvailableCourses)
		{
%>
			<tr>
				<td class="coursesAndUsersTable" id="coursesAndUsersDescription">	
<%
			out.println("<b>Name</b>: " + c.name + "<br/>");
			out.println("<b>Credit pts</b>: " + c.creditPoints + "<br/>");
			out.println("<b>Group number</b>: " + c.groupNumber + "<br/>");
			out.println("<a href='courseDetails.jsp?course=" + c.name + "'>More about this course</a>");
%>
				</td>
				<td class="coursesAndUsersTable" id="coursesAndUsersManagement">

				<form method="get" action="CourseRegistration">
				<input type="hidden" name="course" value="<%= c.name %>"/>
				<input type="hidden" name="operation" value="<%= "Register" %>"/>
				<input type="submit" value="Register" />	
				</form>
				</td>
			</tr>
<%
		}
%>
		</table>
<%
	}
%>
</div>
<jsp:include page="sidebar.jsp"/>
<jsp:include page="footer.html"/>
</body>
</html>